package com.yxhl.zoume.core.func.map;

import android.content.Context;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.yxhl.zoume.common.adapter.BaseAdapter;
import com.yxhl.zoume.core.func.map.info.PoiInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * 用于Poi搜索
 * Created by Xboy on 15/12/4.
 */
public class PoiSearchTask implements PoiSearch.OnPoiSearchListener {

    private Context mContext;
    private BaseAdapter adapter;

    public PoiSearchTask(Context context) {
        mContext = context;
    }

    public PoiSearchTask(Context mContext, BaseAdapter adapter) {
        this.mContext = mContext;
        this.adapter = adapter;
    }

    /**
     * 根据关键字、城市进行Poi搜索
     * created by Xboy 4/12/2015
     *
     * @param keyword
     * @param cityCode
     */
    public void poiSearchByKeywordCity(String keyword, String cityCode) {
        PoiSearch.Query query = new PoiSearch.Query(keyword, "", cityCode);
        query.setPageSize(15);
        query.setPageNum(0);

        PoiSearch poiSearch = new PoiSearch(mContext, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }

    /**
     * 根据PoiInfo进行Poi搜索
     * modified by Xboy 4/12/2015
     *
     * @param entity
     */
    public void poiSearchByPoiInfo(PoiInfo entity) {
        poiSearchByKeywordCity(entity.address, entity.cityCode);
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int resultCode) {
        if (resultCode == 0 && poiResult != null) {
            // 所有的Poi
            ArrayList<PoiItem> pois = poiResult.getPois();
            if (pois == null) {
                return;
            }
            //
            List<PoiInfo> entities = new ArrayList<PoiInfo>();
            for (PoiItem poiItem : pois) {
                PoiInfo entity = new PoiInfo(poiItem.getLatLonPoint().getLatitude(),
                        poiItem.getLatLonPoint().getLongitude()
                        , poiItem.getTitle(), poiItem.getCityName());
                entities.add(entity);
            }

            // TODO: 16/3/21 RRD Xboy ?设置数据
            // 1.设置数据
            // 2.展示
//            adapter.setData(entities);
//            adapter.notifyDataSetChanged();
        }
        //TODO 异常处理
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }
}
